package org.bigjava.buff.goods.service.impl;

import java.util.List;

import org.bigjava.buff.goods.bean.Goods;
import org.bigjava.buff.goods.bean.Type;
import org.bigjava.buff.goods.mapper.TypeMapper;
import org.bigjava.buff.goods.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TypeServiceImpl implements TypeService {

	@Autowired
	private TypeMapper typeMapper;

    public List<Type> findtypeByfid(int fid){
		List<Type> list = typeMapper.findtypeByfid(fid);
		return list;
	}

	public void addfirst(Type type){
		typeMapper.insert(type);
	}

	public void delAll(Integer id) {
		typeMapper.delAll(id);
	}

	public Type getTypeById(Integer id){
    	return typeMapper.getTypeById(id);
	}

	public Goods fillGoodsType(Goods goods) {
		Type goodsType = typeMapper.getTypeById(goods.getGoodsType().getId());
		goodsType.setName(goodsType.getTypeName());
		String typeName = typeMapper.getTypeById(goodsType.getFid()).getTypeName();
		goodsType.setTypeName(typeName);
		goods.getGoodsType().setTypeName(typeName);
		goods.setGoodsType(goodsType);
		return goods;
	}

	//通过一级分类名称来查询出下面的二级分类
	public List<Type> getTypeByTypeName(String typeName) {

    	Type t = typeMapper.getTypeByTypeName(typeName);

    	//查询所有二级分类
    	List list = this.findtypeByfid(t.getId());

		return list;
	}

	//查询出所有的一二级分类
	public List<Type> getAllType() {

    	//查询出所有的二级分类
    	List<Type> towList = typeMapper.getAllTowType();

    	List typeList = this.fillType(towList);

		return typeList;
	}

	//填充类型
	public List<Type> fillType(List<Type> towList){

    	//获取所有的一级类型
		List<Type> oneList = typeMapper.findtypeByfid(0);

    	for(Type t : oneList){
    		for(Type t2 : towList){
    			if(t2.getFid() == t.getId()){
    				t2.setName(t2.getTypeName());
    				t2.setTypeName(t.getTypeName());
				}
			}
		}

    	return towList;
	}


}
